<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 2015/2/3
 * Use : ...
 * File: XcacheSessionHandler.php
 */

namespace ulue\parts\session\handler;


class XcacheSessionHandler extends AbstractSessionHandler
{
    public function __construct($options=[])
    {
        if (!static::isSupported())
        {
            throw new \DRuntimeException('XCache Extension is not available');
        }

        parent::__construct($options);
    }

    static public function isSupported()
    {
        return extension_loaded('xcache');
    }

    public function open($path, $name)
    {
        return true;
    }

    function  read( $id )
    {
        // Check if id exists
        if (!xcache_isset($this->prefix . $id))
        {
            return false;
        }

        return (string) xcache_get($this->prefix . $id);
    }

    function  write( $id ,  $data )
    {
        return xcache_set($this->prefix . $id, $data, ini_get("session.gc_maxlifetime"));
    }

    function  destroy ( $id )
    {
        $id = $this->prefix . $id;

        if (!xcache_isset($id))
        {
            return true;
        }

        return xcache_unset($id);
    }

    public function close()
    {
        return true;
    }

    public function gc($maxlifetime)
    {
        return true;
    }

}// end class XcacheSessionHandler